package com.zmn.plat.admin.controller.orderconfig.orderstock.plat;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.exception.ZmnException;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.orderconfig.orderstock.plat.PlatConfig;

/**
 * 平台配置
 *
 * @author xiangjunming
 * @since 2020年3月17日16 :49:40
 * @modify 20200324 xiangjunming 创建
 */
@Controller
@RequestMapping("/plat/config/")
public class PlatConfigModifyController extends PlatConfigBaseController {

    /**
     * logger 描述此字段
     */
    private Logger logger = LoggerFactory.getLogger(PlatConfigModifyController.class);

    /**
     * 启用/停用
     *
     * @param id
     *            描述此参数
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 20200324 18:18:25
     * @modify 20200324 xiangjunming 创建
     */
    @RequestMapping(value = "updateStatus")
    @ResponseBody
    public ResponseDTO updateStatus(Integer id, Integer status) {
        try {
            PlatConfig platConfig = new PlatConfig();
            platConfig.setId(id);
            platConfig.setStatus(status);
            platConfig.setUpdater(getMcStaff().getRealName());
            platConfig.setUpdateTime(DateUtil.getNow());
            platConfigService.updateByKey(platConfig);
        } catch (ZmnException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 批量启用/停用
     *
     * @param ids
     *            the ids
     * @param status
     *            the status
     * @return the result dto
     * @author xiangjunming
     * @since 2020/07/22
     */
    @RequestMapping(value = "updateStatusByIds")
    @ResponseBody
    public ResponseDTO updateStatusByIds(String ids, int status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择至少一条数据！");
        }
        List<Integer> idList = StringUtil.convertStrsToList(ids);
        platConfigService.updateStatusByIds(idList, status, getMcStaff().getRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 修改库存
     *
     * @param id
     *            描述此参数
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 20200324 18:18:25
     * @modify 20200324 xiangjunming 创建
     */
    @RequestMapping(value = "updateStock")
    @ResponseBody
    public ResponseDTO updateStock(Integer id, Integer value) {
        try {
            if (value == null) {
                value = -1;
            }
            PlatConfig platConfig = new PlatConfig();
            platConfig.setId(id);
            platConfig.setStock(value);
            platConfig.setUpdater(getMcStaff().getRealName());
            platConfig.setUpdateTime(DateUtil.getNow());
            platConfigService.updateByKey(platConfig);
        } catch (ZmnException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 修改节假日系数
     *
     * @param id
     *            描述此参数
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 20200324 18:18:25
     * @modify 20200324 xiangjunming 创建
     */
    @RequestMapping(value = "updateCoefficient")
    @ResponseBody
    public ResponseDTO updateCoefficient(Integer id, Double value) {
        try {
            PlatConfig platConfig = new PlatConfig();
            platConfig.setId(id);
            platConfig.setCoefficient(value);
            platConfig.setUpdater(getMcStaff().getRealName());
            platConfig.setUpdateTime(DateUtil.getNow());
            platConfigService.updateByKey(platConfig);
        } catch (ZmnException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 保存批量设置
     *
     * @param list
     *            the list
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 2020 /04/07 10:43:04
     */
    @RequestMapping(value = "saveBatchSet")
    @ResponseBody
    public ResponseDTO saveBatchSet(@RequestBody List<PlatConfig> list) {
        try {
            String realName = getMcStaff().getRealName();
            platConfigBService.saveBatchSet(list, realName);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }
}
